package com.huawei.appgallery.appcomment.ui.view;

import android.content.Context;
import android.text.Layout.Alignment;
import android.text.SpannableStringBuilder;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;
import huawei.widget.HwTextView;
import o.bcb;
import o.bep;
import o.ContentManager;

public class FoldTextView extends TextView {
    private b ʼ;
    private e ʽ;
    private CharSequence ˊ;
    private CharSequence ˋ;
    private HwTextView ˎ;
    private int ˏ = 5;
    private SpannableStringBuilder ॱ;
    private boolean ᐝ = false;

    public interface b {
        void ॱ(boolean z, HwTextView hwTextView);
    }

    public interface e {
        void ˊ(boolean z, HwTextView hwTextView);
    }

    public FoldTextView() {
        super(ContentManager.getInstant().getContext());
    }

    public FoldTextView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public e ˊ() {
        return this.ʽ;
    }

    public void setListener(e eVar) {
        this.ʽ = eVar;
    }

    public void setOnMaxLineListener(b bVar, HwTextView hwTextView) {
        this.ˎ = hwTextView;
        this.ʼ = bVar;
    }

    public boolean ˏ() {
        return this.ᐝ;
    }

    public void ॱ() {
        if (this.ˋ != null && this.ˊ != null) {
            if (this.ˋ.toString().equals(this.ˊ.toString())) {
                this.ˋ = this.ॱ;
            } else {
                this.ˋ = this.ˊ;
            }
            this.ᐝ = !this.ᐝ;
            if (this.ʽ != null) {
                this.ʽ.ˊ(this.ᐝ, this.ˎ);
            }
            setText(this.ˋ);
        }
    }

    private SpannableStringBuilder ˋ(SpannableStringBuilder spannableStringBuilder, int i, TextPaint textPaint) {
        float measureText = textPaint.measureText(spannableStringBuilder.toString());
        float measureText2 = textPaint.measureText("...");
        float f = 0.0f;
        if (!(this.ˎ == null || TextUtils.isEmpty(this.ˎ.getText()))) {
            f = textPaint.measureText(this.ˎ.getText().toString());
        }
        while (((float) i) < (measureText + measureText2) + f) {
            spannableStringBuilder.delete(spannableStringBuilder.length() - 2, spannableStringBuilder.length() - 1);
            measureText = textPaint.measureText(spannableStringBuilder.toString());
        }
        return spannableStringBuilder;
    }

    public void setOnContentChangedListener(e eVar, HwTextView hwTextView) {
        this.ˎ = hwTextView;
        setListener(eVar);
    }

    public void setContent(SpannableStringBuilder spannableStringBuilder, boolean z, int i) {
        this.ॱ = spannableStringBuilder;
        this.ᐝ = z;
        this.ˋ = null;
        this.ˊ = null;
        try {
            ˎ(i);
        } catch (Exception e) {
            bcb.ˋ.ˊ("MultiLineTextView", "resizeMeasure error:" + e.toString());
        }
    }

    public void setMaxLine(int i) {
        this.ˏ = i;
    }

    private void ˎ(int i) {
        int ˊ = ˊ(i);
        if (TextUtils.isEmpty(this.ॱ) || i <= 0) {
            setText("");
            this.ˊ = this.ॱ;
        } else {
            CharSequence append;
            StaticLayout staticLayout = new StaticLayout(this.ॱ, getPaint(), ˊ, Alignment.ALIGN_NORMAL, getLineSpacingMultiplier(), getLineSpacingExtra(), false);
            int lineCount = staticLayout.getLineCount();
            TextPaint textPaint = new TextPaint();
            textPaint.setTextSize(getTextSize());
            if (lineCount > this.ˏ) {
                if (this.ʼ != null) {
                    this.ʼ.ॱ(true, this.ˎ);
                }
                CharSequence ॱ = bep.ॱ(this.ˏ, staticLayout, this.ॱ);
                SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
                spannableStringBuilder.append(ॱ);
                append = new SpannableStringBuilder().append(this.ॱ.subSequence(0, bep.ˎ(this.ˏ - 1, staticLayout, this.ॱ).length() + ˋ(spannableStringBuilder, ˊ, textPaint).length())).append("...");
            } else {
                if (this.ʼ != null) {
                    this.ʼ.ॱ(false, this.ˎ);
                }
                append = this.ॱ;
            }
            this.ˊ = append;
        }
        if (this.ᐝ) {
            this.ˋ = this.ॱ;
            setText(this.ॱ);
        } else {
            this.ˋ = this.ˊ;
            setText(this.ˊ);
        }
        if (ˊ() != null && this.ˎ != null) {
            ˊ().ˊ(this.ᐝ, this.ˎ);
        }
    }

    private int ˊ(int i) {
        return (i - getPaddingStart()) - getPaddingEnd();
    }
}
